package aishua.com.mytestapplist.datastructure;

import java.util.Arrays;

/**
 * Created by aishua on 2018/5/4.
 * 创建循环队列
 *
 * 1、队列空的情况 head = tail
 * 2、队列满的情况 （tail +1 ) % size = hedd;
 * 3、队列数据长度 （tail -front +size) % size
 */

public class CycleQueue<T> {

    private static final String TAG = "CycleQueue-";
    private int MAX_SIZE =16;
    private int size ; //创建数列的长度(实际就是数组的长度)
    private int front; //指向队头
    private int rear; //指向队尾的下一个
    private Object[] element;

    public CycleQueue(){

        size = MAX_SIZE;
        element = new Object[size];
        front = rear =0;

    }

    public CycleQueue(int size){

        if (size < 0) this.size =MAX_SIZE;
        else this.size = size;
        element = new Object[this.size];
        front = rear =0;
    }

    //返回当前队列的长度
    public int length(){

        return  (rear- front +size) % size;
    }

    public boolean isEmpty(){

        return front == rear ? true :false;
    }

    public boolean isFull(){

        return  (size +1 ) % size ==front;
    }

//    循环队列入队操作
    public void insert(T data){

       if (isEmpty()){
//           队列已经满了，进行扩容处理
           size = element.length;
           element = Arrays.copyOf(element,size);
       }
       element[rear++]=data;
    }




}
